#include "main.h"

void client_write_cp(int CC)
{
	int pox=1;//远端返回值
	struct cp_date wri={0};
	printf("请输入文件名:");
	scanf("%63s",wri.filename);
	while(getchar()!=10);
	if(access(wri.filename,F_OK)==-1){printf("拷贝失败:该文件不存在\n");return;}
	printf("文件存在\n");
	//传输文件信息
	if(stat(wri.filename,&wri.date)==-1){perror("获取文件属性失败");return;}
	printf("传输文件内容\n");
	write(CC,&wri,sizeof(wri));
	read(CC,&pox,4);
	if(pox!=0){printf("远端异常\n");return;}
	//传输文件内容
	FILE*fp=fopen(wri.filename,"r");
	printf("文件打开成功\n");
	long testsize=wri.date.st_size;
	char buf[1024]={0};
	int dd;
	char tt[51]={0};
	while(testsize>0)
	{
		memset(buf,1024,0);
		//printf("开始拷贝—>读\n");
		int ret=fread(buf,1,1024,fp);
		//printf("开始拷贝—>传\n");
		write(CC,buf,ret);
		testsize-=ret;
		dd=(1.0-(float)testsize/wri.date.st_size)*100;
		tt[dd/2]='#';
		tt[50]='\0';
		printf("\r拷贝进度:[%-50s",tt);
		printf("]%d%%",dd);
		/*
		for(int i=0;i<dd;i+=2)
			printf("#");
		printf("]");
		*/
		fflush(stdout);
		usleep(20000);
	}
	printf("\n文件传输完成,文件总大小:%ld\n",wri.date.st_size);

}
